var courseGradeMap = new HashMap();// key:courseId,value:gradeArr[]
var courseRuleMap = new HashMap();// key:courseId,value:timeArr[]
var courseGradeRuleMap = new HashMap();// key:courseId+','+gradeId,value:timeArr[]

function courseUnSchedule() {
    getCourseRule();
    getCourseGradeRule();
    setCourseGradeTree();
}

function getCourseRule() {
    jQuery.ajax({
        type: "GET",
        url: "getUnScheduleGlobalCourseTime?taskId=" + taskId,
        cache: false,
        async: false,
        success: function (msg) {
            var message = JSON.parse(msg);
            var retData = message.data;
            if (retData != null) {
                var length = retData.length;
                for (var i = 0; i < length; i++) {
                    courseRuleMap.put(retData[i].id, retData[i].time
                        .split(','));
                }
            }
        }
    });
}

function getCourseGradeRule() {
    jQuery.ajax({
        type: "GET",
        url: "getUnScheduleCourseTime?taskId=" + taskId,
        cache: false,
        async: false,
        success: function (msg) {
            var message = JSON.parse(msg);
            var retData = message.data;
            if (retData != null) {
                var length = retData.length;
                for (var i = 0; i < length; i++) {
                    courseGradeRuleMap.put(retData[i].id, retData[i].time
                        .split(','));
                }
            }
        }
    });
}

function setCourseGradeTree() {
    jQuery.ajax({
        type: "GET",
        url: "courseAndGrade?taskId=" + taskId,
        cache: false,
        async: false,
        success: function (msg) {
            var message = JSON.parse(msg);
            var retData = message.data;
            if (retData != null) {
                var length = retData.length;
                var root = $("#mainNav_" + unScheduleRuleType);
                var rootHtml = new Array();
                for (var i = 0; i < length; i++) {
                    rootHtml.push("<ul id=" + retData[i].courseId + ">");
                    rootHtml.push("<i class='glyphicon glyphicon-plus-sign' onclick='toggleTree(" + retData[i].courseId + ")'></i>");
                    rootHtml.push("<span id='node_course_" + retData[i].courseId + "' onclick='javascript:getCourseRuleById(" + retData[i].courseId + ")'>" + retData[i].courseName + "</span>");
                    var grades = JSON.parse(retData[i].grades);
                    var gradesLength = grades.length;
                    var gradeArr = new Array();
                    for (var j = 0; j < gradesLength; j++) {
                        rootHtml.push("<li id='node_courseGrade_" + retData[i].courseId + "_" + grades[j].gradeId + "' onclick='javascript:getCourseGradeRuleById(" + retData[i].courseId + "," + grades[j].gradeId + ")'>");
                        rootHtml.push("<span>" + grades[j].gradeName + "</span>");
                        rootHtml.push("</li>");
                        gradeArr.push(grades[j].gradeId);
                    }
                    courseGradeMap.put(retData[i].courseId, gradeArr);
                    rootHtml.push("</ul>");
                }
                root.html(rootHtml.join(""));
            }
        }
    });
}

function getCourseRuleById(courseId) {
    clearTimeTable();
    //设置选中样式
    $("[id*='node_']").removeClass('selected_node');
    $("#node_course_" + courseId).addClass('selected_node');
    //设置选中数据
    selectedNodeType = "course";
    selectedNodeValue = courseId;
    var courseTimeArr = courseRuleMap.get(courseId);
    if (courseTimeArr != null) {
        var length = courseTimeArr.length;
        for (var i = 0; i < length; i++) {
            $("#td_" + courseTimeArr[i]).html("不排课");
        }
    }
    //show tags
    getCourseTagById(courseId);
}

function setCourseRule(courseId, timeIndex, isUnSchedule) {
    /* 同时课程本身也维护timeIndex */
    var courseTimeArr = courseRuleMap.get(courseId);
    if (courseTimeArr == null) {
        if (isUnSchedule == true) {
            courseTimeArr = new Array();
            courseTimeArr.push(timeIndex);
            courseRuleMap.put(courseId, courseTimeArr);
        }
    } else {
        if (isUnSchedule == true) {
            if (!courseTimeArr.contains(timeIndex)) {
                courseTimeArr.push(timeIndex);
            }
        } else {
            if (courseTimeArr.contains(timeIndex)) {
                courseTimeArr.remove(timeIndex);
            }
        }
    }
}

function getCourseGradeRuleById(courseId, gradeId) {
    clearTimeTable();
    //标识选中样式
    $("[id*='node_']").removeClass('selected_node');
    $("#node_courseGrade_" + courseId + "_" + gradeId).addClass('selected_node');
    //标识选中，并显示不排课
    selectedNodeType = "courseGrade";
    selectedNodeValue = courseId + "_" + gradeId;
    //首先显示课程不排课时间，并灰掉
    var courseTimeArr = courseRuleMap.get(courseId);
    if (courseTimeArr != null) {
        var length = courseTimeArr.length;
        for (var i = 0; i < length; i++) {
            $("#td_" + courseTimeArr[i]).html("课程不排课");
            $("#td_" + courseTimeArr[i]).addClass("disabledTd");
            $("#td_" + courseTimeArr[i]).attr("disabled", true);
        }
    }
    //然后显示课程年级不排课时间
    var courseGradeTimeArr = courseGradeRuleMap.get(courseId + "_" + gradeId);
    if (courseGradeTimeArr != null) {
        var length = courseGradeTimeArr.length;
        for (var i = 0; i < length; i++) {
            $("#td_" + courseGradeTimeArr[i]).html("不排课");
        }
    }
    //show tags
    getCourseGradeTagById(courseId,gradeId);
    getCourseTagById(courseId);
}

function setCourseGradeRule(courseIdAndGradeId, timeIndex, isUnSchedule) {
    var arr = courseIdAndGradeId.split('_');
    var courseId = arr[0];
    var gradeId = arr[1];
    var courseGradeTimeArr = courseGradeRuleMap.get(courseId + "_" + gradeId);
    if (courseGradeTimeArr == null) {
        if (isUnSchedule == true) {
            var arr = new Array();
            arr.push(timeIndex);
            courseGradeRuleMap.put(courseId + "_" + gradeId, arr);
        }
    } else {
        if (isUnSchedule == true) {
            if (!courseGradeTimeArr.contains(timeIndex)) {
                courseGradeTimeArr.push(timeIndex);
            }
        } else {
            if (courseGradeTimeArr.contains(timeIndex)) {
                courseGradeTimeArr.remove(timeIndex);
            }
        }
    }
}

function toCourseString() {
    var array = new Array();
    courseRuleMap.forEach(function (key, valueArr) {
        var rule = {};
        rule["id"] = key;
        var temp = $.map(valueArr, function (value, index) {
            return [value];
        });
        if (temp != null && temp.length > 0) {
            rule["time"] = temp.toString();
            rule["type"] = "-";
            array.push(rule);
        }
    })
    return JSON.stringify(array);
}

function toCourseGradeString() {
    var array = new Array();
    courseGradeRuleMap.forEach(function (key, valueArr) {
        var rule = {};
        rule["id"] = key;
        var arr = key.split("_");
        rule["courseId"] = arr[0];
        rule["gradeId"] = arr[1];
        var temp = $.map(valueArr, function (value, index) {
            return [value];
        });
        if (temp != null && temp.length > 0) {
            rule["time"] = temp.toString();
            rule["type"] = "-";
            array.push(rule);
        }
    })
    return JSON.stringify(array);
}